﻿namespace Baddies.GUI
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using Baddies.Nodes;
    using Microsoft.Xna.Framework.Content;    
    using Microsoft.Xna.Framework.Input;    

    /// <summary>
    /// Base class for all GUI nodes. To be attached to a 
    /// input device.
    /// </summary>
    public class GUINode : Node
    {
        /// <summary>
        /// Child Node that reacts to the stimulus first.
        /// </summary>
        private GUINode focusedNode;

        /// <summary>
        /// Gets or sets the focused node.
        /// </summary>
        /// <value>Reference to the focused node.</value>
        [ContentSerializerAttribute(SharedResource = true)]
        public GUINode FocusedNode
        {
            get
            {
                return this.focusedNode;
            }

            set
            {
                Debug.Assert(this.Contains(value), "The node provided is not a child of the InputDevice!");
                this.focusedNode = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the GUINode class.
        /// </summary>
        public GUINode() : base()
        {
        }

        /// <summary>
        /// Reacts to the key provided and consumes the event if
        /// appropiate. To be overrriden in child classes.
        /// </summary>
        /// <param name="key">Key to react to.</param>
        /// <returns>
        /// True if it reacts and consumes the event. False otherwise.
        /// </returns>
        public virtual bool React(Keys key)
        {
            return false;
        }

        /// <summary>
        /// Offers the key to the current focused child.
        /// Utility function.
        /// </summary>
        /// <param name="key">Key to react to.</param>
        /// <returns>
        /// True if it reacts and consumes the event. False otherwise.
        /// </returns>
        public bool FocusedReact(Keys key)
        {
            Debug.Assert(this.focusedNode != null, "Focused child null!");
            return this.focusedNode.React(key);
        }
    }
}
